package fun.eliauk.treedemo;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;


import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;

public class Coder {
    public static void main(String[] args) {


        //TODO 连接mysql信息
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";
        String username = "root";
        String password = "";

        // TODO 表名信息
        String tablePrefix  = ""; //若想去掉表名前缀可写表名前缀
        String[] tablesName ={"user2"};//表名，以字符串数组形式

        //TODO 项目信息
        String author = "xi-zijin";
        // 这块其实就是包名 这样就例如package fun.eliauk.treedemo.controller;
        String packageName = "fun.eliauk";  //java 目录往下写
        String moduleName  = "treedemo";    //


        FastAutoGenerator.create(url,username,password)
                .globalConfig(builder -> {
                    builder.author(author)
                            .disableOpenDir()//生成完是否打开文件
                            .enableSwagger()
                            .commentDate("yyyy-MM-dd")
                            .fileOverride();            //开启覆盖之前生成的文件
                })
                .packageConfig(builder -> {
                    builder.parent(packageName)
                            .moduleName(moduleName)
                            .pathInfo(Collections.singletonMap(OutputFile.xml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(Arrays.stream(tablesName).toList())
                            .addTablePrefix(tablePrefix) // TODO 去掉表名前缀
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .logicDeleteColumnName("deleted")
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            // 映射路径使用连字符格式，而不是驼峰
                            .enableHyphenStyle()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            //生成通用的resultMap
                            .enableBaseResultMap()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateConfig(new Consumer<TemplateConfig.Builder>() {
                    @Override
                    public void accept(TemplateConfig.Builder builder) {
                        // 实体类使用我们自定义模板
                        builder.entity("templates/EntityTemplate.java");
                    }
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
